package org.six11.olive3.server;

import static org.six11.olive3.server.DatabaseThing.*;

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.six11.util2.Debug;

import static org.six11.olive3.server.WebActionServlet.*;

public class SuggestHandler extends WebActionHandler {

  public final static String PARAM_DESCRIPTION = "description";

  public SuggestHandler(String jspPage, WebActionServlet webActionServlet) {
    super(jspPage, webActionServlet);
  }

  public void service(HttpServletRequest req, HttpServletResponse resp, ServletContext context)
      throws ServletException, IOException {
    String description = req.getParameter(PARAM_DESCRIPTION);
    if (description != null && description.length() > 0) {
      HttpSession session = req.getSession();
      String uid = (String) session.getAttribute(SESSION_USER_ID);
      webActionServlet.getDbThing().insertRow(SqlStrings.INSERT_DESCRIPTION, description, uid);
      req.setAttribute(MSG_INFO, "Successfully added description. Add another if you want.");
      Debug.out("SuggestHandler", "Added initial description by user " + uid + ": '" + description
          + "; ");
    }
    go(req, resp, context);
  }

}
